package hu.village.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import hu.village.mapper.CommitteeMapper;
import hu.village.pojo.Account;
import hu.village.pojo.PageBean;
import hu.village.pojo.User;
import hu.village.pojo.Village;
import hu.village.service.CommitteeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class CommitteeServiceImpl implements CommitteeService {

    @Autowired
    private CommitteeMapper committeeMapper;
    @Override
    public void bindingVillage(Village village) {
        village.setState(1);
        committeeMapper.bindingVillage(village);
    }

    @Override
    public void submit(Account account) {
        account.setState(1);
        account.setVisible(1);
        LocalDateTime time = LocalDateTime.now();
        account.setCreateTime(time);
        account.setUpdateTime(time);
        log.info("新增账目信息,{}",account);

        committeeMapper.insertAccount(account);
    }

    @Override
    public void updateAccount(Account account) {
        LocalDateTime time = LocalDateTime.now();
        account.setUpdateTime(time);
        committeeMapper.updateAccount(account);
    }

    @Override
    public PageBean selectHistoryAccount(Account account,Integer page,Integer pageSize) {
        //设置分页参数
        PageHelper.startPage(page,pageSize);
        //执行查询
        //账目超过30天visible变为0
        account.setVisible(0);
        List<Account> accountList = committeeMapper.selectAccountByVisible(account);
        //获取分页结果
        Page<Account> p = (Page<Account>) accountList;
        //封装PageBean对象
        return new PageBean(p.getTotal(),p.getResult());
    }

    @Override
    public List<User> selectApplication() {
        //提交申请的用户状态是1
        Integer status = 1;
        List<User> userList = committeeMapper.selectUserApplication(status);
        return userList;
    }

    @Override
    public void processApplication(Integer choice,Integer id) {
        int status = 1;
        if(choice == 1){
            status = 2;
        }else {
            status = 0;
        }
        committeeMapper.processApplication(status,id);
    }
}
